package com.efeichong.oss;

import com.google.common.collect.ImmutableMap;

/**
 * @author lxk
 * @date 2020/11/5
 * @description 获取文件类型
 */
public class FileType {
    /**
     * 未知类型
     */
    public final static String DEFAULT_OBJECT_CONTENT_TYPE = "application/otcet-stream";
    /**
     * 定义几种常用的类型
     */
    public final static ImmutableMap<String, String> TYPE_MAP = new ImmutableMap.Builder()
            .put(".jpg", "image/jpg")
            .put(".gif", "image/gif")
            .put(".jpeg", "image/jpeg")
            .put(".png", "image/png")
            .put(".ico", "image/x-icon")
            .put(".mp4", "video/mpeg4")
            .put(".wav", "audio/wav")
            .put(".avi", "video/avi")
            .put(".bmp", "application/x-bmp")
            .put(".css", "text/css")
            .put(".txt", "text/plain")
            .put(".html", "text/html")
            .put(".htm", "text/html")
            .put(".mp3", "audio/mp3")
            .put(".xls", "application/vnd.ms-excel")
            .put(".doc", "application/msword")
            .put(".ppt", "application/vnd.ms-powerpoint")
            .put(".pdf", "application/pdf")
            .put(".xml", "text/xml")
            .build();


    /**
     * 获取content-type
     *
     * @param suffix 文件后缀
     * @return
     */
    public static String getContentType(String suffix) {
        String contentType = TYPE_MAP.get(suffix);
        if (contentType == null) {
            return DEFAULT_OBJECT_CONTENT_TYPE;
        } else {
            return contentType;
        }
    }

    /**
     * 是否包含文件类型
     *
     * @param suffix
     * @return
     */
    public static boolean contains(String suffix) {
        return TYPE_MAP.containsKey(suffix);
    }
}
